@page "/Admin/Role"
@using BootstrapBlazor.Components

<PageTitle>角色管理</PageTitle>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">

            <AdminTable2 TItem="RoleEntity" Context="item" PageSize="20" TableTd99Width="330"
                
                OnQuery="OnQuery" InitQuery="InitQuery" OnEdit="OnEdit">

                <TableHeader>
                    <th>名称</th>
                    <th>描述</th>
                    <th>管理员</th>
                </TableHeader>
                <TableRow>
                    <td>@item.Name</td>
                    <td>@item.Description</td>
                    <td><Switch Value="item.IsAdministrator" OnColor="Color.Success" OnText="" OffText="" IsDisabled="true" /></td>
                </TableRow>
                <TableTd99>
                    <button @onclick="e => BeginAllocUsers(item)" type="button" class="mr-2 btn btn-light btn-xs"><i class="fa fa-user-secret"></i>分配用户</button>
                    <button @onclick="e => BeginAllocMenus(item)" type="button" class="mr-2 btn btn-light btn-xs" disabled="@item.IsAdministrator"><i class="fa fa-user-secret"></i>分配菜单</button>
                </TableTd99>
                <EditTemplate>
                    <div class="row">
                        <div class="form-group col-12">
                            <label class="form-label">名称</label>
                            <input @bind="item.Name" type="text" class="form-control" placeholder="" maxlength="255">
                        </div>
                        <div class="form-group col-12">
                            <label class="form-label">描述</label>
                            <input @bind="item.Description" type="text" class="form-control" placeholder="" maxlength="255">
                        </div>
                    </div>
                </EditTemplate> 
            </AdminTable2>
        </div>
    </div>
</div>

<AllocTable2 TItem="RoleEntity" TChild="UserEntity" @bind-Item="@allocItemUsers" ChildProperty="Users" Title="@($"【分配用户】{allocItemUsers?.Name}")">
    <TableTd1>@context.Username</TableTd1>
</AllocTable2>

<AllocTable2 TItem="RoleEntity" TChild="MenuEntity" @bind-Item="@allocItemMenus" ChildProperty="Menus" Title="@($"【分配菜单】{allocItemMenus?.Name}")">
    <TableTd1>@context.Label</TableTd1>
</AllocTable2>

@code {

    [Inject] IAggregateRootRepository<RoleEntity> repository { get; set; }

    async Task InitQuery(AdminQueryInfo e)
    {
        e.Filters = new AdminFilterInfo[]
        {
        };
        await Task.Yield();
    }
    void OnQuery(AdminQueryEventArgs<RoleEntity> e)
    {
        e.Select.WhereIf(!e.SearchText.IsNull(), a => a.Name.Contains(e.SearchText) ||
                a.Description.Contains(e.SearchText));
    }

    async Task OnEdit(RoleEntity item)
    {
        await Task.Yield();
    }

    RoleEntity allocItemUsers;
    [AdminButton("alloc_users")]
    void BeginAllocUsers(RoleEntity item) => allocItemUsers = item;

    RoleEntity allocItemMenus;
    [AdminButton("alloc_menus")]
    void BeginAllocMenus(RoleEntity item) => allocItemMenus = item;
}
